package com.example.task1.ui.AllService.movies;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.MoviesSeatBean;
import com.example.task1.bean.MoviesSeatDetailsBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;

public class MoviesSeatActivity extends AppCompatActivity {

    private int cinemaId;
    private int movieId;
    private RecyclerView rvList;
    private int mRoomId, mRoomIds;
    private MoviesSeatAdapter moviesSeatAdapter;
    private List<Integer> ids = new ArrayList<>();
    private Button btnSubMit;
    private int lodPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movies_seat);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("选择座位");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        cinemaId = getIntent().getIntExtra("id", 0);//影院id
        movieId = getIntent().getIntExtra("ids", 0);//影片id
        mRoomId = getIntent().getIntExtra("roomId", 0);//场次id
        mRoomIds = getIntent().getIntExtra("roomIds", 0);//影厅id
        initView();
        initData();
        setAdapter();
        business();
    }

    private void business() {
        moviesSeatAdapter.setOnSelectItem(new MoviesSeatAdapter.OnSelectItem() {
            @Override
            public void Item(MoviesSeatBean.DataDTO date, int position, BaseAdapter.myHolder holder) {
                lodPosition = position;
                holder.itemView.setBackgroundColor(Color.RED);
                moviesSeatAdapter.notifyDataSetChanged();
                Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/movie/theatre/seat/list?roomId="
                        + mRoomIds + "&row=" + date.getRow() + "&col=" + date.getCol());
                Api.send(apiRespCall, MoviesSeatDetailsBean.class, new CallBack<MoviesSeatDetailsBean>() {
                    @Override
                    public void success(MoviesSeatDetailsBean data) {
                        ids.add(date.getId());
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
        btnSubMit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (Integer id : ids) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("movieId",movieId);
                    map.put("seatId",id);
                    map.put("roomId",mRoomIds);
                    map.put("theaterId",cinemaId);
                    map.put("timesId",mRoomId);
                    Call<ApiResp> apiRespCall = Api.api().post(MyApplication.TOKEN, "/prod-api/api/movie/ticket",Api.getBody(map));
                    Api.send(apiRespCall, ApiResp.class, new CallBack<ApiResp>() {
                        @Override
                        public void success(ApiResp data) {
                            MyApplication.show("支付成功");
                            Intent intent = new Intent(MoviesSeatActivity.this,MoviesActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }

                        @Override
                        public void error(String msg) {

                        }
                    });

                }
            }
        });
    }

    private void setAdapter() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 16, RecyclerView.HORIZONTAL, false);
        moviesSeatAdapter = new MoviesSeatAdapter();
        moviesSeatAdapter.setHasStableIds(true);

        rvList.setLayoutManager(gridLayoutManager);
        rvList.setAdapter(moviesSeatAdapter);
    }

    private void initData() {
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/movie/theatre/list4times?id=" + mRoomId + "&roomId=" + cinemaId);
        Api.send(apiRespCall, MoviesSeatBean.class, new CallBack<MoviesSeatBean>() {
            @Override
            public void success(MoviesSeatBean data) {
                moviesSeatAdapter.setData(data.getData());
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


    private void initView() {
        rvList = findViewById(R.id.rv_list);
        btnSubMit = (Button) findViewById(R.id.btn_subMit);
    }
}